<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.client.helper');
jimport('joomla.application.component.model');

class AuctionModelEdit extends JModel
{
	
	var $_id = null;
	var $_table = '#__auction';
	var $_lists = '';
	var $_data = null;
	
	function __construct()
	{
		parent::__construct();
		$array = JRequest::getVar('cid',  0, '', 'array');
		
		$this->setId((int)$array[0]);
		
	}

	function setId($id)
	{
		$this->_id	= $id;
	}
	
	function &getData()
	{
		
		if (empty( $this->_data )) {
			$query = ' SELECT * FROM #__auction'.
					'  WHERE id = '.$this->_id;
					
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();	
		
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0; 
			$this->_data->product_id = 0; 
			$this->_data->auction_date = null; 
			$this->_data->currentprice = 0; 
			$this->_data->winprice = 0; 
			$this->_data->winid = null; 
			$this->_data->published = null; 
			$this->_data->closed = 0; 
		}
		return $this->_data;
	}
	
	function getOptCar($current){
		$query = "SELECT product_id FROM #__auction WHERE closed = 0 AND product_id != ".$current;
		$this->_db->setQuery($query);
		$proId = $this->_db->loadResultArray();
		if($proId){
			$proStr = implode(',', $proId);
			$query = "SELECT id, model, katno FROM #__pr_product WHERE type = 1 AND approve = 1 AND bought = 0 AND id NOT IN (".$proStr.") AND published = 1";
		} else {
			$query = "SELECT id, model, katno FROM #__pr_product WHERE type = 1 AND approve = 1 AND bought = 0  AND published = 1";
		}
		$this->_db->setQuery($query);
		$pros = $this->_db->loadObjectList();
		$opt = "";
		foreach($pros as $pro){
			if($pro->id == $current){
				$opt .= '<option value="'.$pro->id.'" selected="selected">'.$pro->katno.' - '.$pro->model.'</option>';
			} else {
				$opt .= '<option value="'.$pro->id.'">'.$pro->katno.' - '.$pro->model.'</option>';
			}
		}
		
		return $opt;
	}

	function store()	{
		$array = JRequest::getVar('cid',  0 , '', 'array');

		global $mainframe;
		$row =& $this->getTable();
		if(!$row->bind(JRequest::get('post'))){
			echo '<script>alert("'.$row->getError().'");window.history.go(-1);</script>\n';
			exit();
		}
		$row->product_id  = JRequest::getVar('product_id', '','post');
		$row->auction_date  = JRequest::getVar('auction_date','','post');
		$row->desiredprice  = JRequest::getVar('desiredprice','','post');
		$row->currentprice  = JRequest::getVar('currentprice','','post');
		/*$config =& JFactory::getConfig();
		$tzoffset = $config->getValue('config.offset');
		
		$date =& JFactory::getDate($row->auction_date, $tzoffset);
		$row->auction_date = $date->toMySQL();*/		
		
		$row->published = intval(JRequest::getVar('published'));
		
		//print_r($date);exit();
		// pre-save checks
		if (!$row->check()) {
		JError::raiseError(500, $row->getError() );
		}
		
		if(!$row->store()){
			echo '<script>alert("'.$row->getError().'");window.history.go(-1);</script>\n';
			exit();
		}
		
		return true;
	}

	function delete()
	{
		global $mainframe;
		$cid = JRequest::getVar('cid',array(),'','array');
		$db = JFactory::getDBO();
		if(count($cid)> 0 ){
			$cids = implode(',',$cid);	
			
			$query = "Delete From #__auction Where id in ($cids)";
			$db->setQuery($query);
			if(!$db->query()){
				echo "<script> alert('".$db->getErrorMsg()."');window.history.go(-1); </script>\n";
			}
		}

		return true;
	}
	
	function deleteOrders()
	{
		global $mainframe;
		$cid = JRequest::getVar('cid',array(),'','array');
		$db = JFactory::getDBO();
		if(count($cid)> 0 ){
			$cids = implode(',',$cid);	
			
			$query = "DELETE FROM #__users_pay WHERE id IN ($cids)";
			$db->setQuery($query);
			if(!$db->query()){
				echo "<script> alert('".$db->getErrorMsg()."');window.history.go(-1); </script>\n";
			}
		}

		return true;
	}

	function publish($publish) {
		
		$cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		JArrayHelper::toInteger($cid);
	
		if ( count( $cid ) < 1 ) {
			$action = $publish ? 'Publish' : 'Unpublish';
			JError::raiseError(500, JText::_( 'Select Item to '.$action, true ) );
		}
		
		$cids = implode( ',', $cid );
		$count = count( $cid );
		$query = 'UPDATE #__auction'
		. ' SET published = '.(int) $publish
		. ' WHERE id IN ( '.$cids.' )'
		;

		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		if (!$db->query()) {
			JError::raiseError(500, $db->getErrorMsg() );
		}
	
	}
	
	function show($show) {
		
		$cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		JArrayHelper::toInteger($cid);
	
		if ( count( $cid ) < 1 ) {
			$action = $publish ? 'Show' : 'Unshow';
			JError::raiseError(500, JText::_( 'Select Item to '.$action, true ) );
		}
		
		$cids = implode( ',', $cid );
		$count = count( $cid );
		$query = 'UPDATE #__auction'
		. ' SET show_status = '.(int) $show
		. ' WHERE id IN ( '.$cids.' )'
		;

		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		if (!$db->query()) {
			JError::raiseError(500, $db->getErrorMsg() );
		}
	
	}

}

?>